package app

import (
	"APT/internal/dao"
	"APT/internal/library/contexts"
	"APT/internal/model"
	"APT/internal/model/entity"
	"APT/internal/model/input/input_basics"
	"APT/internal/model/input/input_form"
	"APT/internal/model/input/input_hotel"
	"APT/internal/service"
	"APT/utility/format"
	"context"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"

	"APT/api/app/basics"
)

func (c *ControllerBasics) HomeDateLoad(ctx context.Context, req *basics.HomeDateLoadReq) (res *basics.HomeDateLoadRes, err error) {
	var (
		pmsBanner    *entity.PmsBanner
		MemberInfo   *model.MemberIdentity
		AlertMessage []*entity.PmsNotify
	)
	res = new(basics.HomeDateLoadRes)
	if res.Banner, _, err = service.BasicsBanner().List(ctx, &input_basics.PmsBannerListInp{
		PageReq: input_form.PageReq{
			Page:       1,
			PerPage:    10,
			Pagination: false,
		},
		Model:    "BANNER",
		Language: contexts.GetLanguage(ctx),
	}); err != nil {
		return
	}
	if res.Property, _, err = service.HotelService().PropertyList(ctx, &input_hotel.PmsPropertyListInp{
		PageReq: input_form.PageReq{
			Page:       1,
			PerPage:    100,
			Pagination: false,
		},
		IsFindClose: true,
	}); err != nil {
		return
	}

	PmsBanner := dao.PmsBanner.Ctx(ctx).
		Where(dao.PmsBanner.Columns().Model, "EVENTS").
		Where(dao.PmsBanner.Columns().Language, contexts.GetLanguage(ctx))

	if ghttp.RequestFromCtx(ctx).GetHeader("X-Version") == "1.1.15" {
		PmsBanner = PmsBanner.WhereLike(dao.PmsBanner.Columns().BannerName, "immovables_%")
	} else {
		PmsBanner = PmsBanner.Where(dao.PmsBanner.Columns().BannerStatus, 1)
	}

	if err = PmsBanner.Scan(&pmsBanner); err != nil {
		return
	}
	if !g.IsEmpty(pmsBanner) {
		res.ImmovablesCover = pmsBanner.BannerImage
		res.ImmovablesPath = pmsBanner.Path
		res.ImmovablesChain = pmsBanner.Chain
	}
	MemberInfo = contexts.GetMemberUser(ctx)
	if !g.IsEmpty(MemberInfo) {
		// 查询首页弹窗信息
		if err = dao.PmsNotify.Ctx(ctx).
			Where(dao.PmsNotify.Columns().NotifyType, "GIFT").
			Where(dao.PmsNotify.Columns().IsRead, "N").
			Where(dao.PmsNotify.Columns().MemberId, MemberInfo.Id).Scan(&AlertMessage); err != nil {
			return
		}

		if len(AlertMessage) > 0 {
			for _, v := range AlertMessage {
				res.AlertMessage = append(res.AlertMessage, &basics.HomeDateAlertMessage{
					Id:            v.Id,
					NotifyType:    v.NotifyType,
					NotifyContent: v.NotifyData.Get(contexts.GetLanguage(ctx)).String(),
				})
			}
		}

	}

	// 查询短租模式最小预定区间是多少
	if res.MinDaysNotice, err = dao.PmsProperty.Ctx(ctx).
		Where(dao.PmsProperty.Columns().LeaseClose, 1).
		Min(dao.PmsProperty.Columns().MinDaysNotice); err != nil {
		return
	}
	if res.MinDaysNotice < 7 {
		res.MinDaysNotice = 7
	}
	return
}

func (c *ControllerBasics) HomeDateConfig(ctx context.Context, req *basics.HomeDateConfigReq) (res *basics.HomeDateConfigRes, err error) {
	res = new(basics.HomeDateConfigRes)
	if err = dao.PmsAppconfig.Ctx(ctx).Where(dao.PmsAppconfig.Columns().Language, contexts.GetLanguage(ctx)).Scan(&res.ConfigList); err != nil {
		return
	}
	for k, v := range res.ConfigList {
		res.ConfigList[k].Value = format.GetHtmlImgSrc(v.Value)
	}
	return
}
func (c *ControllerBasics) ReadAlertMessage(ctx context.Context, req *basics.ReadAlertMessageReq) (res *basics.ReadAlertMessageRes, err error) {
	if _, err = dao.PmsNotify.Ctx(ctx).WherePri(req.Id).Update(g.Map{
		dao.PmsNotify.Columns().IsRead: "Y",
	}); err != nil {
		return
	}
	return
}
func (c *ControllerBasics) HomeDeviceInfo(ctx context.Context, req *basics.HomeDeviceInfoReq) (res *basics.HomeDeviceInfoRes, err error) {
	var (
		MaxDeviceInfo *entity.PmsScanDevice
		MaxDeviceSn   int
	)
	if err = dao.PmsScanDevice.Ctx(ctx).Fields(dao.PmsScanDevice.Columns().DeviceSn).OrderDesc(dao.PmsScanDevice.Columns().DeviceSn).Scan(&MaxDeviceInfo); err != nil {
		err = gerror.Wrap(err, "获取设备最大排序，请稍后重试！")
		return
	}
	if MaxDeviceInfo == nil {
		MaxDeviceSn = 10001
	} else {
		MaxDeviceSn = MaxDeviceInfo.DeviceSn + 1
	}
	if _, err = dao.PmsScanDevice.Ctx(ctx).
		Data(g.Map{
			dao.PmsScanDevice.Columns().DeviceSn: MaxDeviceSn,
		}).OmitEmptyData().Insert(); err != nil {
		err = gerror.Wrap(err, "新增设备码，请稍后重试！")
	}
	res = new(basics.HomeDeviceInfoRes)
	res.DeviceSn = MaxDeviceSn
	return
}
